<?php
$cfg_limit=10;
$cfg_domain="elearning.com";
function condb(){
	/*
	$host='localhost';
	$user='ielearn';
	$pass='5767b';
	$dbname='ielearn';
	*/
	$user='root';
	$pass='';
	$dbname='elearn';
	
	if(mysql_connect($host,$user,$pass)){
		mysql_select_db($dbname)or die(mysql_error());
		mysql_query("set names utf8")or die(mysql_error()); 
		mysql_query("set character_set_results=utf8")or die(mysql_error());
	}else{
		echo "Mysql Connection Error";
	}
}
function redir($url='index.php',$delaysec=0){
		echo "<meta http-equiv=\"REFRESH\" content=\"$delaysec;URL=$url\" >";
}
	function DateThai($strDate)
	{
		$strYear = date("Y",strtotime($strDate))+543;
		$strMonth= date("n",strtotime($strDate));
		$strDay= date("j",strtotime($strDate));
		$strHour= date("H",strtotime($strDate));
		$strMinute= date("i",strtotime($strDate));
		$strSeconds= date("s",strtotime($strDate));
		$strMonthCut = Array("","ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค.","มิ.ย.","ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค.");
		$strMonthThai=$strMonthCut[$strMonth];
		return "$strDay $strMonthThai $strYear";
	}
function validmail($email){
	if(preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email))return true;
	else return false;
}
function pagemenu($pageurl,$page,$allpage){
	echo "<center>";
	  if($page!=1)echo "<a href=$pageurl&page=1>|&lt;</a> &nbsp;";
	  if($page!=1)echo "<a href=$pageurl&page=".($page-1).">&lt;</a> &nbsp;";
	  for($i=1;$i<=$allpage;++$i){
		if($i!=$page)
			echo "<a href=$pageurl&page=$i>$i</a> &nbsp;";
		else
			echo "$i &nbsp;";
	  }
	  if($page!=$allpage)echo "<a href=$pageurl&page=".($page+1).">&gt;</a> &nbsp;";
	  if($page!=$allpage)echo "<a href=$pageurl&page=$allpage>&gt;|</a> &nbsp;";
	echo "</center>";
} 
function cropMaster($source, $target, $thumbwidth,$thumbheight) {
		 $image_info = getImageSize($source) ;
		 $iw=$image_info[0];
		 $ih=$image_info[1];
		 if ($iw!=$thumbwidth && $ih!=$thumbheight) {
			if($iw==$ih){
				$w=$thumbwidth;
				$h=$thumbheight;
			}else if($iw>$ih){
				$w=$thumbwidth;
				$h=round($w*($ih/$iw));
			}else if($ih>$iw){
				$h=$thumbheight;
				$w=round(($iw/$ih)*$h);
			}	   
			  switch ($image_info['mime']) {
			  case 'image/gif':
				   if (imagetypes() & IMG_GIF)  { 
						$o_im = imageCreateFromGIF($source) ;
				   } else {
						$ermsg = 'GIF images are not supported<br />';
				   }
				   break;
			  case 'image/jpeg':
				   if (imagetypes() & IMG_JPG)  {
						$o_im = imageCreateFromJPEG($source) ;
				   } else {
						$ermsg = 'JPEG images are not supported<br />';
				   }
				   break;
			  case 'image/png':
				   if (imagetypes() & IMG_PNG)  {
						$o_im = imageCreateFromPNG($source) ;
				   } else {
						$ermsg = 'PNG images are not supported<br />';
				   }
				   break;
			  case 'image/wbmp':
				   if (imagetypes() & IMG_WBMP)  {
						$o_im = imageCreateFromWBMP($source) ;
				   } else {
						$ermsg = 'WBMP images are not supported<br />';
				   }
				   break;
			  default:
				   $ermsg = $image_info['mime'].' images are not supported<br />';
				   break;
			  }
	   
			  if (!isset($ermsg)) {
				   $o_wd = imagesx($o_im) ;
				   $o_ht = imagesy($o_im) ;
				   $t_im = ImageCreateTrueColor($w, $h); 
				   ImageCopyResampled($t_im, $o_im, 0, 0, 0, 0, $w+1, $h+1, $o_wd, $o_ht);
				   ImageJPEG($t_im, $target);
				   imageDestroy($o_im);
				   imageDestroy($t_im);
			  }
		 } else {
			  copy($source, $target);
		 }
		 return isset($ermsg)?$ermsg:NULL;
}
function cropImage( $source , $target , $w , $h ){
	$size = getImageSize( $source );
	$iw = $size[0];
	$ih = $size[1];
	$left=($iw-$w)/2;
	$top=($ih-$h)/2;
		 
	switch ( $size[mime] )
	{
	case 'image/gif':
		$o_im = imageCreateFromGIF( $source );
		break;
	case 'image/jpeg':
		$o_im = imageCreateFromJPEG( $source );
		break;
	case 'image/png':
		$o_im = imageCreateFromPNG( $source );
		break;
	default:
		$error = $size[mime].' ไม่รองรับ.';
		break;
	};
	if ( $error == '' )
	{
		$t_im=imagecreatetruecolor($w, $h);
		ImageCopyResampled($t_im, $o_im, 0, 0, $left, $top, $w, $h,$w,$h);
		ImageJPEG($t_im, $target);
		imageDestroy($o_im);
		imageDestroy($t_im);
	}else{
		return $error;
	}
}
?>